var windowName = "serviceCreateOrder";
var Page, imageBrowser, TaskPics
apiready = function () {
    onWindowShow(function () {
        var address = getLocData("curAddress");
        logJson(address)
        if (isTrue(address)) {
            Page.address = address
        }
        ;
        var contact = getLocData("curContact");
        if (isTrue(contact)) {
            Page.contact = contact
        }
        ;
    })
    onWindowHide(function () {
    })
    console.log("index page is OK");
    TaskPics = new PicUp()
    imageBrowser = api.require('imageBrowser')
    Page = createVM("#Page", {
        //页面数据
        id: 0
        ,tips:""
        ,product:{}
        , address: {}
        , contact: {}
        , bookAt: ""
        , pics: []
        , picsV: []
    }, {
        //页面方法
        "init": function () {
            var data = getData4Page()
            this.id = data.id
            var product = data.product
            this.product = product

        }

        , submit: function () {
            if (!isTrue(this.address)) {
                showToast("请选择地址");
                return
            }
            if (!isTrue(this.contact)) {
                showToast("请选择联系人");
                return
            }
            if (!isTrue(this.bookAt)) {
                showToast("请选择预约时间");
                return
            }
            // if (!isTrue(this.pics)) {
            //     showToast("请上传附件资料");
            //     return
            // }
            var data = {
                tips:this.tips
                ,bookAt:this.bookAt
                ,pics:this.pics
            }
            apiPost(
                "/app/user/create/orders/"
                + this.id + "/"
                + this.address.id + "/"
                + this.contact.id
                , data
                , function (res,code) {
                    logJson(res)
                    if (code == 200){
                        showToast(res.message);
                        goto("servicesPay",{order:res.data});
                    }

                })


        }
        , baiduditu: baiduditu

        //图片选择器相关
        , selectPic: function () {
            var Page = this;
            TaskPics.select(9).setCbFunc(function (pics) {
                logJson(pics, "pics")
                Page.picsV = TaskPics.getViewData()
                Page.pics = TaskPics.getData()
                if (Page.pics.length > 2) {
                    Page.scrollXTo("#box", ".s1", Page.pics.length - 1);
                }

            })
        },
        del: function (index) {
            var pics = TaskPics.removePic(index)
            Page.picsV = TaskPics.getViewData()
            Page.pics = TaskPics.getData()
            // this.en.pics = TaskPics.getData()
        },
        move: function (index, num) {
            var pics = TaskPics.picMove(index, num)
            Page.picsV = TaskPics.getViewData()
            Page.pics = TaskPics.getData()
            // this.en.pics = TaskPics.getData()
        },
        showPic: function (index, pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex: index
            });
        }
    })
    Page.init()
}
